function [hfile] = prt_Concatenate(hfile, hfile2, nvol, tr)
% PRT::Concatenate  - concatenate two PRTs in time
%
% FORMAT:       [prt] = prt.Concatenate(prt2, tvol, tr)
%
% Input fields:
%
%       prt2        PRT object used for concatenation
%       nvol        number of volumes to shift
%       tr          tr
%
% Output fields:
%
%       prt         altered PRT object
%
% Note: conditions that are only present in 2nd PRT will be added

% Version:  v0.8a
% Build:    9102503
% Date:     Oct-25 2009, 3:48 AM CEST
% Author:   Jochen Weber, SCAN Unit, Columbia University, NYC, NY, USA
% URL/Info: http://wiki.brainvoyager.com/BVQXtools

% argument check
if nargin < 4 || ...
    numel(hfile) ~= 1 || ...
   ~isBVQXfile(hfile, 'prt') || ...
    numel(hfile2) ~= 1 || ...
   ~isBVQXfile(hfile2, 'prt') || ...
    numel(nvol) ~= 1 || ...
   ~isa(nvol, 'double') || ...
    isinf(nvol) || ...
    isnan(nvol) || ...
    nvol <= 0 || ...
    numel(tr) ~= 1 || ...
   ~isa(tr, 'double') || ...
    isinf(tr) || ...
    isnan(tr) || ...
    tr <= 0
    error( ...
        'BVQXfile:BadArgument', ...
        'Invalid call to %s.', ...
        mfilename ...
    );
end
bc1 = bvqxfile_getcont(hfile.L);
bc2 = bvqxfile_getcont(hfile2.L);
if ~strcmpi(bc1.ResolutionOfTime, bc2.ResolutionOfTime)
    error( ...
        'BVQXfile:BadArgument', ...
        'Protocols must have same ResolutionOfTime setting.' ...
    );
end
if lower(bc1.ResolutionOfTime) == 'm'
    tdiff = nvol * tr;
else
    tdiff = nvol;
end
ncon1 = numel(bc1.Cond);
ncon2 = numel(bc2.Cond);

% get names
names1 = cell(1, ncon1);
names2 = cell(1, ncon2);
for nc = 1:ncon1
    names1{nc} = bc1.Cond(nc).ConditionName{1};
end
for nc = 1:ncon2
    names2{nc} = bc2.Cond(nc).ConditionName{1};
end

% iterate over conditions of 2nd PRT
for nc = 1:ncon2
    
    % find match
    match = strcmpi(names2{nc}, names1);
    
    % if match
    if any(match)
        match = find(match);
        match = match(1);
        
        % take care of weights
        if numel(bc1.Cond(match).Weights) ~= size(bc1.Cond(match).OnOffsets, 1)
            bc1.Cond(match).Weights = ones(size(bc1.Cond(match).OnOffsets, 1), 1);
        end
        if numel(bc2.Cond(nc).Weights) ~= size(bc2.Cond(nc).OnOffsets, 1)
            bc2.Cond(nc).Weights = ones(size(bc2.Cond(nc).OnOffsets, 1), 1);
        end
        
        % add to equally named condition with offset
        bc1.Cond(match).OnOffsets = ...
            [bc1.Cond(match).OnOffsets; bc2.Cond(nc).OnOffsets + tdiff];
        bc1.Cond(match).NrOfOnOffsets = size(bc1.Cond(match).OnOffsets, 1);
        bc1.Cond(match).Weights = ...
            [bc1.Cond(match).Weights(:); bc2.Cond(nc).Weights(:)];
        
    % otherwise
    else
        
        % add new condition and shift onsets
        bc1.Cond(end+1) = bc2.Cond(nc);
        bc1.Cond(end).OnOffsets = bc1.Cond(end).OnOffsets + tdiff;
    end
    
end

% store into old structure
bc1.NrOfConditions = numel(bc1.Cond);
bvqxfile_setcont(hfile.L, bc1);
